package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Result;
import com.itheima.service.BusinessDataService;
import com.itheima.service.MemberService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/report")
@ResponseBody
//会员数量统计折线图
public class MemberController {

    @Reference
    private MemberService memberService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport() {
        //会员数量集合
        Map<String, Object> map = new HashMap<>();
        //月份集合
        List<String> months = new ArrayList<>();
        //获取本地时间
        Calendar calendar = Calendar.getInstance();
        //当前时间倒推到第一个会员注册的时间的时间
        calendar.add(Calendar.MONTH, -12);
        for (int i = 0; i < 12; i++) {
            //循环遍历获取每个月的会员注册时间
            calendar.add(Calendar.MONTH, 1);
            Date date = calendar.getTime();
            //2019.03时间格式
            months.add(new SimpleDateFormat("yyyy.MM").format(date));
        }

        map.put("months", months);
        List<Integer> memberCount = memberService.findMemberByMonths(months);
        map.put("memberCount", memberCount);

        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
    }

     public class ReportExcelController {
        @Autowired
        private BusinessDataService businessDataService;

        @RequestMapping("/exportBusinessReport")
        public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws Exception {
            Map<String, Object> result = businessDataService.findBusinessReport();
            String reportDate = (String) result.get("today");
            Integer todayNewMember = (Integer) result.get("todayNewMember");
            Integer totalMember = (Integer) result.get("totalMember");
            Integer thisWeekNewMember = (Integer) result.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) result.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) result.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) result.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) result.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) result.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) result.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) result.get("thisMonthVisitsNumber");
            Integer remark = (Integer) result.get("remark");
            List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");

            String businessPath = request.getSession().getServletContext().getRealPath("businessData") + File.separator + "report_businessData.xlsx";
            XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(businessPath)));
            XSSFSheet sheet = workbook.getSheetAt(0);

            XSSFRow row = sheet.getRow(2);
            row.getCell(5).setCellValue(reportDate);

            row = sheet.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            row.getCell(7).setCellValue(totalMember);

            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            row.getCell(7).setCellValue(thisMonthNewMember);

            row = sheet.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            row.getCell(7).setCellValue(todayVisitsNumber);

            row = sheet.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            row.getCell(7).setCellValue(thisWeekVisitsNumber);

            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            row.getCell(7).setCellValue(thisMonthVisitsNumber);

            row = sheet.getRow(10);
            row.getCell(5).setCellValue(remark);
            int rowNum = 12;
            for (Map map : hotSetmeal) {
                String name = (String) map.get("name");
                Long setmeal_count = (Long) map.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map.get("proportion");
                row = sheet.getRow(rowNum++);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmeal_count);
                row.getCell(6).setCellValue(proportion.doubleValue());
                row.getCell(7).setCellValue(remark);
            }
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("application/vnd.ms‐excel");
            response.setHeader("content‐Disposition", "attachment;filename=report.xlsx");
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();
            workbook.close();
            return null;
        }
    }
}
